/* 
 * Arcms，一个基于J2EE架构内容管理系统
 * Copyright © Zrar Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://www.zrar.com/arcms
 */
package org.grayrabbit.cms.service.user.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.dao.user.UserDao;
import org.grayrabbit.cms.entity.OperLog;
import org.grayrabbit.cms.entity.User;
import org.grayrabbit.cms.service.user.UserService;
import org.grayrabbit.cms.vo.user.UserBean;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.service.impl.GenericServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class UserServiceImpl extends GenericServiceImpl implements UserService {
	/**
	 * 注入dao层：
	 */
	@Autowired
	@Qualifier("userDaoImpl")
	private UserDao userDao;
	
	/**
	 * 此方法执行时为只读事务：
	 * @see UserService#getOperLog(String)
	 */
	@Transactional(readOnly=true)
	public User getUser(final String userId){
		User user = userDao.findById(userId);
		userDao.initialize(user);
		return user;
	}
	
	/**
	 * @see UserService#delete(String)
	 */
	@Transactional
	public void delete(final User user) {
		userDao.makeTransient(user);
	}
	
	/**
	 * @see UserService#save(OperLog)
	 */
	@Transactional
	public void save(final User user) {
		userDao.makePersistent(user);		
	}
	
	/**
	 * 此方法执行时为只读事务：
	 * @see UserService#getPage(UserBean)
	 */
	@Transactional(readOnly=true)
	public Page<User> getPage(final UserBean userBean){
		Page<User> page =  new Page<User>();
		page.setTotal(userDao.count(userBean));
		page.setResult(userDao.findByQc(userDao.assembly(userBean)));
		return page;
	}

	/**
	 * 此方法执行时为只读事务：
	 * @see UserService#list(Page, UserBean)
	 */
	public void list(Page<User> page, UserBean userBean) {
		if(page==null)
			return;
		if(page.isAutoCount()){
			page.setTotal(userDao.count(userBean));
		}
		page.setResult(userDao.findByPage(page,userBean));
	}
	
	/**
	 * 根据主键id列删除，批量删除OperLog
	 * @see UserService#delete(String[])
	 */
	public void delete(final String[] key) {
		int i =0;
		for(String id : key){
			userDao.makeTransient(userDao.findById(id));
			i++;
			if(i==MAX_CLEAR)
				userDao.clear();
		}
	}
	
	public Boolean checkNameUnique(String userLoginName,String userId){
		return userDao.checkNameUnique(userLoginName,userId);
	}
	
	/**
	 * 登陆名登陆验证判断
	 * @param user
	 * @param LoginType
	 * @return
	 */
	public User getUserByLoginName(User user){
		List<User> userList = userDao.getUserByLoginName(user.getUserLoginName());
		if(userList==null||userList.isEmpty()){
			return null;
		}else if(userList.size()>1){
			return null;
		}else{
			return userList.get(0);
		}
	}
	/**
	 * 邮箱登陆验证判断
	 * @param user
	 * @return
	 */
	public User getUserByEmail(User user){
		List<User> userList = userDao.getUserByEmail(user.getUserLoginName());
		if(userList==null||userList.isEmpty()){
			return null;
		}else if(userList.size()>1){
			return null;
		}else{
			return userList.get(0);
		}
	}

	public String getUserRealName(String loginName) {
		if(StringUtils.isNotBlank(loginName)){
			List<User> userList = userDao.getUserByLoginName(loginName);
			if(userList!=null&&userList.size()==1){
				return userList.get(0).getUserRealName();
			}
		}
		return loginName;
	}
	

	public boolean addBlacker(String blacker,String userId){
			User user=userDao.findById(userId);
			if(StringUtils.isNotBlank(user.getBlacklist())&&(user.getBlacklist().contains(blacker+";"))){
				return true;
			}
			StringBuffer blackRemain=new StringBuffer();
			List<User> users=userDao.getUserByLoginName(blacker);
			
			if(users!=null&&!users.isEmpty()){
				if(StringUtils.isNotBlank(user.getBlacklist())){
					blackRemain.append(user.getBlacklist()+blacker+";");
				}else{
					blackRemain.append(";"+blacker+";");
				}
				user.setBlacklist(blackRemain.toString());
				userDao.makePersistent(user);
			}
			return true;
		
	}
	
	
	public void addBlackers(String[] blackers,String userId){
		User user=userDao.findById(userId);
		StringBuffer blackRemain=new StringBuffer();
		if(StringUtils.isNotBlank(user.getBlacklist())){
			for(String str:blackers){
				if(!user.getBlacklist().contains(str+";")){
					List<User> users=userDao.getUserByLoginName(str);
					if(users!=null&&users.isEmpty()){
						blackRemain.append(str+";");
					}
				}
			}
		}
		blackRemain.append(";"+user.getBlacklist());
		user.setBlacklist(blackRemain.toString());
		userDao.makePersistent(user);
		
	}
	
	
	public void  removeBlacker(String blacker,String userId){
		User user=userDao.findById(userId);
		String[] blackers=null;
		if(StringUtils.isNotBlank(user.getBlacklist())){
			if(user.getBlacklist().contains(blacker)){
				blackers=user.getBlacklist().split(";");
				List<String> remain=new ArrayList<String>();
				for(String str:blackers){
					if(!str.equals(blacker)){
						remain.add(str);
					}
				}
				StringBuffer blackRemain=new StringBuffer();
				for(String str:remain){
					blackRemain.append(str+";");
				}
				user.setBlacklist(blackRemain.toString());
				userDao.makePersistent(user);
			}
		}
	}

	public int countUserNumber(UserBean user) {
		return userDao.count(user);
	}

}